package com.dave.functionquote;

import com.dave.start.Apple;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Supplier;

/**
 * @Author Dave
 * @Date 2018/8/9
 * @Description 方法引用
 */
public class Quote {

    public static Quote getQuote(Supplier<Quote> supplier) {
        return supplier.get();
    }


    /**
     * 创建苹果
     *
     * @param biFunction
     * @param apples
     * @return
     */
    private List<Apple> createApples(BiFunction<Integer, String, Apple> biFunction, Map<String, Integer> apples) {
        List<Apple> appleList = new ArrayList<>();
        Set<String> keys = apples.keySet();
        keys.forEach(e -> {
            Apple apply = biFunction.apply(apples.get(e), e);
            appleList.add(apply);
        });
        return appleList;
    }

    public static void main(String[] args) {

        List<Apple> apples = Arrays.asList(
                new Apple(150, Apple.COLOR_GREEN),
                new Apple(100, Apple.COLOR_RED),
                new Apple(200, Apple.COLOR_RED)
        );


        //Supplier T get() 使用ClassName::new 来引用
        Quote quote = getQuote(Quote::new);
        Supplier<Apple> apple = Apple::new;

        //静态引用
        apples.forEach(System.out::println);

        //实例引用 逆序 如果两个一样重就比较颜色
        apples.sort(Comparator
                .comparing(Apple::getWeight)
                .reversed()
                .thenComparing(Apple::getColor));

        //创建苹果对象
        Map<String, Integer> appMap = new HashMap<>();
        appMap.put(Apple.COLOR_RED, 100);
        appMap.put(Apple.COLOR_GREEN, 200);
        List<Apple> appleList = quote.createApples(Apple::new, appMap);
    }
}
